package com.ycfmedical.bed.ui.fragment;

import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.components.LimitLine;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineDataSet;
import com.ycfmedical.bed.Constants;
import com.ycfmedical.bed.R;
import com.ycfmedical.bed.listener.Receivedata;
import com.ycfmedical.bed.model.ChartManage;
import com.ycfmedical.bed.present.VsHistoryPresenter;
import com.ycfmedical.bed.utils.MyXFormatter;
import com.ycfmedical.bed.utils.TimeUtil;
import com.ycfmedical.bed.widgets.VsSelectTimeDialog;

import java.util.ArrayList;
import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import cn.droidlover.xdroidmvp.mvp.XFragment;

/**
 * 生命体征历史数据界面
 * Created by Administrator on 2017-04-10.
 */

public class VsHistoryFragment extends XFragment<VsHistoryPresenter> implements Receivedata {

    @BindView(R.id.sleep_embed)     //深睡
    TextView sleepEmbed;
    @BindView(R.id.sleep_commonly)  //中睡
    TextView sleepCommonly;
    @BindView(R.id.sleep_badly)     //浅睡
    TextView sleepBadly;
    @BindView(R.id.sleep_sober)     //清醒
    TextView sleepSober;
    @BindView(R.id.sleepStatus)     //睡眠容器
    LinearLayout sleepStatus;

    private HashMap<String,TextView> texts = new HashMap<String,TextView>();
    private Unbinder unbinder;
    private String Time, name;
    private ArrayList<TextView> dayTextViews;
    private ArrayList<TextView> yearTextViews;
    private ArrayList<Entry> entrys = new ArrayList<>();
    private ChartManage chartManage;
    private static final int MIN_CLICK_DELAY_TIME = 1000;
    private static long lastClickTime;


    @BindView(R.id.vs_history_lineChart)
    LineChart lineChart;

    @BindView(R.id.vs_text_average)
    TextView text_average;

    @BindView(R.id.show_type)
    TextView show_type;
    @BindView(R.id.vs_text_historyName)
    TextView text_historyName;
    @BindView(R.id.text_day_one)
    TextView day_one;
    @BindView(R.id.one)
    TextView one;
    @BindView(R.id.two)
    TextView two;
    @BindView(R.id.five)
    TextView five_;
    @BindView(R.id.three)
    TextView three;
    @BindView(R.id.text_year_one)
    TextView year_one;
    @BindView(R.id.text_day_two)
    TextView day_two;
    @BindView(R.id.text_year_two)
    TextView year_two;
    @BindView(R.id.text_day_three)
    TextView day_three;
    @BindView(R.id.text_year_three)
    TextView year_three;
    @BindView(R.id.text_day_four)
    TextView day_four;
    @BindView(R.id.text_year_four)
    TextView year_four;
    @BindView(R.id.text_day_five)
    TextView day_five;
    @BindView(R.id.text_year_five)
    TextView year_five;
    @BindView(R.id.text_more)
    TextView more;
    @BindView(R.id.text_five)
    LinearLayout five;

    @Override
    public void initData(Bundle savedInstanceState) {
        dayTextViews = new ArrayList<TextView>();
        yearTextViews = new ArrayList<TextView>();
        Bundle bundle = getArguments();
        Time = (String) bundle.get("time");
        name = (String) bundle.get("name");
        addText();
        settext();
        initChart();
        getP().initPresenter();
        Receive(Constants.NEWTIME,Constants.NEWID);
    }

    private void initChart() {
        chartManage = new ChartManage();
        switch (name) {
            case "心率":
                chartManage.addChart(lineChart);
                chartManage.setX(0, 0f, 100f, true);
                chartManage.setY(0, 0f, 120f, false);
                chartManage.setLineData(0, entrys,false);
                chartManage.setLine(0,  Color.rgb(47, 201, 27), false, LineDataSet.Mode.HORIZONTAL_BEZIER);
                chartManage.getLineCharts().get(0).getAxisLeft().setEnabled(true);
                chartManage.getLineCharts().get(0).getXAxis().setDrawGridLines(false);

                YAxis yAxisHr = lineChart.getAxisLeft();
                yAxisHr.addLimitLine(chartManage.getLimitLine(100, "高", Color.RED, Color.GRAY, LimitLine.LimitLabelPosition.RIGHT_TOP));
                yAxisHr.addLimitLine(chartManage.getLimitLine(60, "低", Color.GREEN, Color.GRAY, LimitLine.LimitLabelPosition.RIGHT_BOTTOM));
                sleepStatus.setVisibility(View.GONE);

                one.setVisibility(View.GONE);
                two.setVisibility(View.GONE);
                three.setVisibility(View.GONE);
                five_.setVisibility(View.GONE);
                break;
            case "呼吸":
                chartManage.addChart(lineChart);
                chartManage.setX(0, 0f, 100f, true);
                chartManage.setY(0, 0f, 50f, false);
                chartManage.setLineData(0, entrys,false);
                chartManage.setLine(0, Color.rgb(27, 88, 201), false, LineDataSet.Mode.HORIZONTAL_BEZIER);
                chartManage.getLineCharts().get(0).getAxisLeft().setEnabled(true);
                chartManage.getLineCharts().get(0).getXAxis().setDrawGridLines(false);

                YAxis yAxisBt = lineChart.getAxisLeft();
                yAxisBt.addLimitLine(chartManage.getLimitLine(25, "高", Color.RED, Color.GRAY, LimitLine.LimitLabelPosition.RIGHT_TOP));
                yAxisBt.addLimitLine(chartManage.getLimitLine(14, "低", Color.GREEN, Color.GRAY, LimitLine.LimitLabelPosition.RIGHT_BOTTOM));
                sleepStatus.setVisibility(View.GONE);

                one.setVisibility(View.GONE);
                two.setVisibility(View.GONE);
                three.setVisibility(View.GONE);
                five_.setVisibility(View.GONE);
                break;
            case "温度":
                chartManage.addChart(lineChart);
                chartManage.setX(0, 0f, 100f, true);
                chartManage.setY(0, 32f, 42f, false);
                chartManage.setLineData(0, entrys,false);
                chartManage.setLine(0, Color.rgb(209, 74, 25), false, LineDataSet.Mode.HORIZONTAL_BEZIER);
                chartManage.getLineCharts().get(0).getAxisLeft().setEnabled(true);
                chartManage.getLineCharts().get(0).getXAxis().setDrawGridLines(false);
                YAxis yAxisTp = lineChart.getAxisLeft();
                yAxisTp.addLimitLine(chartManage.getLimitLine(39, "高", Color.RED, Color.GRAY, LimitLine.LimitLabelPosition.RIGHT_TOP));
                yAxisTp.addLimitLine(chartManage.getLimitLine(36, "低", Color.GREEN, Color.GRAY, LimitLine.LimitLabelPosition.RIGHT_BOTTOM));
                sleepStatus.setVisibility(View.GONE);

                one.setVisibility(View.GONE);
                two.setVisibility(View.GONE);
                three.setVisibility(View.GONE);
                five_.setVisibility(View.GONE);
                break;
            case "睡眠":
                chartManage.addChart(lineChart);
                chartManage.setX(0, 0f, 100f, true);
                chartManage.setY(0, 50f, 120f, false);
                chartManage.getLineCharts().get(0).getAxisLeft().setEnabled(false);
                chartManage.getLineCharts().get(0).getAxisRight().setEnabled(false);
                chartManage.getLineCharts().get(0).getXAxis().setDrawGridLines(false);
                chartManage.inverted(0, true);
                break;
        }
    }

    private void addText() {
        dayTextViews.add(day_one);
        dayTextViews.add(day_two);
        dayTextViews.add(day_three);
        dayTextViews.add(day_four);
        dayTextViews.add(day_five);

        yearTextViews.add(year_one);
        yearTextViews.add(year_two);
        yearTextViews.add(year_three);
        yearTextViews.add(year_four);
        yearTextViews.add(year_five);

        texts.put("深睡",sleepEmbed);
        texts.put("中睡",sleepCommonly);
        texts.put("浅睡",sleepBadly);
        texts.put("清醒",sleepSober);
    }

    private void settext() {
        year_one.setText(Time.substring(0, 5));
        day_one.setText(Time.substring(5));
        if (name.equals("睡眠")) {
            text_historyName.setText("睡眠时长:");
        } else {
            text_historyName.setText("平均" + name);
        }
        for (int i = 0; i < 5; i++) {
            dayTextViews.get(i).setText(TimeUtil.getSpecifiedDayAfter(Time, i).substring(5));
            yearTextViews.get(i).setText(TimeUtil.getSpecifiedDayAfter(Time, i).substring(0, 5));
        }
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_vs_history;
    }

    @Override
    public VsHistoryPresenter newP() {
        return new VsHistoryPresenter();
    }

    @OnClick({R.id.btn_back, R.id.text_one, R.id.text_two, R.id.text_three, R.id.text_four, R.id.text_five, R.id.text_more})
    public void onClick(View v) {
        if (isFastClick()) {
            switch (v.getId()) {
                case R.id.btn_back:
                    getFragmentManager().popBackStackImmediate();
                    break;
                case R.id.text_one:
                    Receive(Constants.NEWTIME,Constants.NEWID);
                    break;
                case R.id.text_two:
                    Receive(Constants.FORMERLYTIME,Constants.FORMERLYID);
                    break;
                case R.id.text_three:
                    Toast.makeText(context,"没有数据",Toast.LENGTH_SHORT).show();
                    break;
                case R.id.text_four:
                    Toast.makeText(context,"没有数据",Toast.LENGTH_SHORT).show();
                    break;
                case R.id.text_five:
                    Toast.makeText(context,"没有数据",Toast.LENGTH_SHORT).show();
                    break;
                case R.id.text_more:
                    new VsSelectTimeDialog(this).show(getFragmentManager(), "test-simple-calendar");

                    break;
                default:
                    break;
            }
        }
    }

    @Override
    public void Receive(String data,String userId) {
        String s1 = data.replaceAll("年", "-");
        String s2 = s1.replaceAll("月", "-");
        String s3 = s2.replaceAll("日", "");
        getP().ask(s3, name,userId);
    }

    private boolean isFastClick() {
        /**
         * 防止用户点击过快产生误操作
         * */
        boolean flag = false;
        long curClickTime = System.currentTimeMillis();
        if ((curClickTime - lastClickTime) >= MIN_CLICK_DELAY_TIME) {
            flag = true;
        }
        lastClickTime = curClickTime;
        return flag;
    }

    public String getName() {
        return name;
    }

    public ArrayList<Entry> getEntrys() {
        return entrys;
    }

    public ChartManage getChartManage() {
        return chartManage;
    }

    public TextView getText_average() {
        return text_average;
    }

    public TextView getShow_type() {
        return show_type;
    }

    public HashMap<String, TextView> getTexts() {
        return texts;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        unbinder = ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
